using System;
using System.Collections.Generic;
using System.Linq;
using Nop.Core.Caching;
using Nop.Core.Data;
using Nop.Plugin.Remote.Vishnu.Domain;

namespace Nop.Plugin.Remote.Vishnu.Services
{
    public partial class OrderFlagService : IOrderFlagService
    {
        #region Fields

        private readonly IRepository<OrderFlagRecord> _ofRepository;
        private readonly ICacheManager _cacheManager;

        #endregion

        #region Ctor

        public OrderFlagService(ICacheManager cacheManager,
            IRepository<OrderFlagRecord> sbwRepository)
        {
            this._cacheManager = cacheManager;
            this._ofRepository = sbwRepository;
        }

        #endregion

        #region Methods


        public virtual void DeleteOrderFlag(OrderFlagRecord orderFlagRecord)
        {
            if (orderFlagRecord == null)
                throw new ArgumentNullException("orderFlagRecord");

            _ofRepository.Delete(orderFlagRecord);
        }

        public virtual IList<OrderFlagRecord> GetAll()
        {
            var query = from of in _ofRepository.Table
                        select of;
            var records = query.ToList();
            return records;
        }

        public virtual OrderFlagRecord Find(int orderId)
        {
            var query = from of in _ofRepository.Table
                        where of.OrderId == orderId
                        orderby of.FlaggedBy descending
                        select of;

            return query.ToList().FirstOrDefault();

        }

        public virtual OrderFlagRecord Find(int orderId, int flagId)
        {
            var query = from of in _ofRepository.Table
                        where of.OrderId == orderId && of.FlagId == flagId
                        orderby of.FlaggedBy descending
                        select of;

            return query.ToList().FirstOrDefault();
        }

        public virtual OrderFlagRecord GetById(int orderFlagRecordId)
        {
            if (orderFlagRecordId == 0)
                return null;

            var record = _ofRepository.GetById(orderFlagRecordId);
            return record;
        }

        public virtual void InsertOrderFlag(OrderFlagRecord orderFlagRecord)
        {
            if (orderFlagRecord == null)
                throw new ArgumentNullException("orderFlagRecord");

            _ofRepository.Insert(orderFlagRecord);
        }

        public virtual void UpdateOrderFlag(OrderFlagRecord orderFlagRecord)
        {
            if (orderFlagRecord == null)
                throw new ArgumentNullException("orderFlagRecord");

            _ofRepository.Update(orderFlagRecord);
        }

        #endregion
    }
}
